<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin\Sys;

use App\Controller\AbstractController;
use App\Middleware\AuthMiddleware;
use App\Model\Tag;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;

#[Middlewares([AuthMiddleware::class])]
#[Controller]
class SysController extends AbstractController
{
    #[GetMapping(path: '/sys/dashboard')]
    public function dashboard()
    {
        $data = '{
    "articleCount": 95,
    "userCount": 31,
    "messageCount": 800,
    "visitCount": 2596,
    "contributionData": [
        {
            "date": "2024-02-19",
            "count": 1
        },
        {
            "date": "2024-03-14",
            "count": 1
        },
        {
            "date": "2024-03-20",
            "count": 1
        },
        {
            "date": "2024-03-25",
            "count": 1
        },
        {
            "date": "2024-03-26",
            "count": 1
        },
        {
            "date": "2024-04-03",
            "count": 2
        },
        {
            "date": "2024-04-09",
            "count": 1
        },
        {
            "date": "2024-04-12",
            "count": 1
        },
        {
            "date": "2024-04-17",
            "count": 1
        },
        {
            "date": "2024-04-19",
            "count": 1
        },
        {
            "date": "2024-04-23",
            "count": 1
        },
        {
            "date": "2024-04-26",
            "count": 1
        },
        {
            "date": "2024-04-28",
            "count": 1
        },
        {
            "date": "2024-04-29",
            "count": 1
        },
        {
            "date": "2024-05-07",
            "count": 2
        },
        {
            "date": "2024-05-08",
            "count": 1
        },
        {
            "date": "2024-05-10",
            "count": 1
        },
        {
            "date": "2024-05-13",
            "count": 1
        },
        {
            "date": "2024-05-15",
            "count": 1
        },
        {
            "date": "2024-05-17",
            "count": 1
        },
        {
            "date": "2024-10-13",
            "count": 1
        },
        {
            "date": "2024-11-27",
            "count": 1
        },
        {
            "date": "2025-01-21",
            "count": 1
        },
        {
            "date": "2025-01-23",
            "count": 1
        },
        {
            "date": "2025-02-08",
            "count": 1
        }
    ]
}';
        return success(json_decode($data));
    }

    #[GetMapping(path: '/sys/dashboard/bottom')]
    public function bottom()
    {
        $tags = Tag::query()
            ->selectRaw('sys_tag.name, COUNT(sys_article.id) as value')
            ->leftJoin('article_tag', 'tag.id', '=', 'article_tag.tag_id')
            ->leftJoin('article', 'article_tag.article_id', '=', 'article.id')
            ->groupBy('tag.id', 'tag.name')
            ->get();

        return success($tags);
    }
}
